package org.yuyang.oschina_tabhostdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    FragmentTabHost mTabHost;
    LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab_layout);
        initView();
    }

    private void initView() {
        mInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        initTabs();
    }

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        for (int i = 0; i < tabs.length; i++) {
            if (i == 2) continue;
            MainTab tab = tabs[i];
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getResNameId()));

            View tabView = mInflater.inflate(R.layout.tab_item_view, null);
            ImageView tabImg = (ImageView) tabView.findViewById(R.id.tabimg);
            TextView tabText = (TextView) tabView.findViewById(R.id.tabtext);
            tabImg.setImageResource(tab.getResIconId());
            tabText.setText(getString(tab.getResNameId()));

            tabSpec.setIndicator(tabView);
            mTabHost.addTab(tabSpec, tab.getClz(), null);
        }
    }
}
